<?php

namespace App\Http\Controllers\Backend;

use App\Models\AdminPermission;
use App\Models\AdminRole;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class RoleController extends Controller
{
    //角色列表
    public function index()
    {
        $this->authorize('role-list');
        $roles=AdminRole::paginate(10);
        return view('backend.role.index',compact('roles'));
    }
    //角色创建
    public function create()
    {
        $this->authorize('role-list');
        $permissions=AdminPermission::all();
        return view('backend.role.add',compact('permissions'));
    }
    //角色添加行为
    public function store()
    {
        //验证
        $this->validate(\request(),[
            'name'=>'required|alpha|min:2|unique:admin_roles',
            'description'=>'required|min:2',
            'permissions' => 'required|array'
        ]);
        $permissions=\request('permissions');
        //逻辑
        if($adminrole=AdminRole::create(\request(['name','description']))){
            foreach ($permissions as $permission){
                $adminrole->permissions()->attach($permission);
            }
        }
        //渲染
        return redirect(admin_prefix().'/roles');
    }
    //角色编辑
    public function edit(AdminRole $role)
    {
        $this->authorize('role-list');
        //获取所有的权限
        $permissions=AdminPermission::all();
        //当前角色所拥有的权限
        $rolepermission=$role->permissions;

        return view('backend.role.edit',compact('role','permissions','rolepermission'));
    }
    //角色编辑行为
    public function update(AdminRole $role)
    {
        $this->authorize('role-list');
        //验证
        $this->validate(\request(),[
            'name' => 'required|alpha|min:2|unique:admin_roles,name,'.$role->id,
            'description'=>'required|min:2',
            'permissions' => 'required|array'
        ]);
        $permission=AdminPermission::findMany(\request('permissions'));
        $rolepermission=$role->permissions;//当前角色已存在的权限
        //逻辑
        DB::transaction(function() use ($role,$permission,$rolepermission){

            AdminRole::where('id',$role->id)->update(\request(['name','description']));
            //增加的权限
            $addpermissions=$permission->diff($rolepermission);
            if(!$addpermissions->isEmpty()){
                $role->grantPermission($addpermissions);
            }
            //要删除的权限
            $delpermissions=$rolepermission->diff($permission);
            if(!$delpermissions->isEmpty()){
                $role->deletePermission($delpermissions);
            }
        });
        //渲染
        return redirect(admin_prefix().'/roles');
    }
    //角色删除
    public function destroy()
    {
        $this->authorize('role-list');
        //删除角色以及角色对应的权限
        $ids=is_array(\request('id'))?\request('id'):[\request('id')];

        DB::transaction(function () use ($ids){
            //删除关联关系
            foreach ($ids as $id){
                $adminrole=AdminRole::find($id);
                $adminrole->deletePermission($adminrole->permissions->pluck('id'));
            }
            //删除用户
            AdminRole::whereIn('id',$ids)->delete();
        });

        return ['status' => '1', 'code' => 200];
    }
}
